package com.zhuss.core.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.config.type.UpdateConfigTypeBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.ConfigTypeDao;
import com.zhuss.core.db.entity.ConfigType;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.OceanException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;

/**
 * 创建配置类型 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/20 21:55
 */
@Component("configTypeUpdate")
public class ConfigTypeUpdate extends AbstractProcessor<Object, UpdateConfigTypeBo> implements Processor<Object, UpdateConfigTypeBo> {

    @Autowired
    private ConfigTypeDao configTypeDao;
    
    @Autowired
	@Qualifier("configTypeRead")
	private ConfigTypeRead configTypeRead;
    
    /**
     * 校验
     */
    @Override
    public void check(UpdateConfigTypeBo bo) throws OceanException {
    	Long configTypeId = bo.getConfigTypeId();
    	// String type = bo.getType();
    	String value = bo.getValue();
    	Integer sort = bo.getSort();
    	
    	if (value == null || "".equals(value.trim())) {
    		throw new OceanException(ExceptionCode.配置类型value为空);
    	}
    	if (sort == null || sort < 0) {
    		// throw new OceanException(ExceptionCode.配置类型sort为空);
    	}
    	
    	
    	List<ConfigType> list = configTypeRead.listNotIdByValue(value, configTypeId);
    	if (!list.isEmpty()) {
    		throw new OceanException(ExceptionCode.配置类型value已存在);
    	}
    	
    	List<ConfigType> sortList = configTypeRead.listNotIdBySort(sort, configTypeId);
    	Integer maxSort = configTypeRead.getMaxSort();
    	if (!sortList.isEmpty()) {
    		throw new OceanException(ExceptionCode.配置类型顺序已存在, String.valueOf(maxSort));
    	}
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(UpdateConfigTypeBo param) {
    	ConfigType record = new ConfigType();
    	record.setConfigTypeId(param.getConfigTypeId());
    	record.setType(param.getType());
    	record.setValue(param.getValue());
    	record.setSort(param.getSort());
    	
    	record.setStatus(param.getStatus());
    	
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	configTypeDao.updateByPrimaryKeySelective(record);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(UpdateConfigTypeBo param) throws OceanException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.配置类型修改.key());
    	record.setTargetId(param.getConfigTypeId());
    	record.setTargetName(param.getType() + ":" + param.getValue());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
